﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eTMS.Controllers;
using eTMS.Controllers.KhoaHocService;
using eTMS.Controllers.KhoaService;
using eTMS.Controllers.NganhHocService;

namespace eTMS.Forms.Students
{
    public partial class FrmAddNewRangeLop : eTMS.Controls.BaseForm
    {
        public FrmAddNewRangeLop()
        {
            InitializeComponent();
        }

        private LopController controller;

        private string _MaNamHoc = "";
        private KhoaHocEntity _KhoaHoc;
        private NganhHocEntity _Nganh ;
        private KhoaEntity _Khoa;
        public string MaNamHoc
        {
            set
            {
                _MaNamHoc = value;
            }
        }
        public KhoaEntity Khoa
        {
            set
            {
                _Khoa = value;
                txtTenKhoa.Text = value.TenKhoa;
            }
        }
        public NganhHocEntity NganhHoc
        {
            set
            {
                _Nganh = value;
                txtTenNganh.Text = value.TenNganhHoc;
            }
        }
        public void SetController(LopController lopController)
        {
            controller = lopController;
        }
        public KhoaHocEntity KhoaHoc
        {
            set
            {
                _KhoaHoc = value;
                txtMaKhoaHoc.Text = value.TenKhoaHoc;
            }
        }
        private List<string> GetTenLop()
        {
            string[] names = new string[7];
            int[] pos = new int[7];
            names[0] = txtTienTo.Text.Trim();
            pos[0] = 0;
            if (cbkNganhHoc.Checked)
            {
                names[1] = _Nganh.MaNganhHoc;
            }
            else
            {
                names[1] = "";
            }
            pos[1] = (int)txtNganh.Value;

            if (cbkKhoa.Checked)
            {
                names[2] = _Khoa.MaKhoa;
            }
            else
            {
                names[2] = "";
            }
            pos[2] = (int)txtKhoa.Value;

            if (cbkKhoaHoc.Checked)
            {
                names[3] = _KhoaHoc.MaKhoaHoc;
            }
            else
            {
                names[3] = "";
            }
            pos[3] = (int)txtKhoaHoc.Value;

            if (cbkNamHoc.Checked)
            {
                names[4] = _MaNamHoc;
            }
            else
            {
                names[4] = "";
            }
            pos[4] = (int)txtNamHoc.Value;

            pos[5] = (int)txtViTri.Value;

            names[5] = "auto_number_\r\n";

            names[6] = txtHauTo.Text;
            pos[6] = 6;
            for (int i = 0; i < pos.Length - 1; i++)
            {
                for (int j = i + 1; j < pos.Length; j++)
                {
                    if (pos[i] > pos[j])
                    {
                        int itmp = pos[i];
                        pos[i] = pos[j];
                        pos[j] = itmp;
                        string stmp = names[i];
                        names[i] = names[j];
                        names[j] = stmp;
                    }
                }
            }
            int posAuto = 0;
            for (int i = 0; i < pos.Length; i++)
            {
                if (names[i] == "auto_number_\r\n")
                {
                    posAuto = i;
                    break;
                }
            }
            List<string> ls = new List<string>();
            int count = (int)txtSoLop.Value;
            int start = (int)txtBatDau.Value;
            int len = (int)txtDoDai.Value;
            for (int i = 0; i < count; i++)
            {
                string name = "";
                names[posAuto] = Commons.Common.FixLength(Convert.ToString(start + i), len);
                for(int j=0;j<names.Length;j++)
                {
                    if(names[j].Length>0)
                    {
                        name = name+ txtPhanCach.Text+ names[j];
                    }
                }
                if (name.Length > 0)
                    name.Substring(txtPhanCach.Text.Length);
                ls.Add(name);
            }
            return ls;
        }
        private void FrmAddNewRangeLop_Load(object sender, EventArgs e)
        {

        }

        private void ucCommandDialog1_OnOK(object sender, CancelEventArgs e)
        {
            if(txtSoLop.Value<=0)
            {
                ShowWarning("Số lớp cần tạo phải là số nguyên dương!");
                e.Cancel = true;
                return;
            }
            List<string> ls = GetTenLop();
            if(controller.CheckExistsMaLop(ls,_Nganh.NganhHoc_ID,_KhoaHoc.KhoaHoc_ID))
            {
                ShowWarning("Các mã lớp mới có mã đã tồn tại. Bạn hãy kiểm tra lại");
                return; 
            }
            controller.AddRangeLop(ls, _Nganh.NganhHoc_ID, _KhoaHoc.KhoaHoc_ID, (int)txtSoSVToiDa.Value);
        }
    }
}
